package hdd.config;

import hdd.MassageDrivenBean.UserMDB;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.apache.activemq.spring.ActiveMQConnectionFactory;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.*;
import org.springframework.core.env.Environment;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.listener.DefaultMessageListenerContainer;
import org.springframework.jms.listener.adapter.MessageListenerAdapter;

import javax.jms.ConnectionFactory;

//@Configuration
@PropertySource({"classpath:setting.properties"})
@ComponentScan(basePackages = "hdd.MassageDrivenBean")
public class JMSConfig implements EnvironmentAware {
    private Environment environment;


    //真正可以产生Connection的ConnectionFactory，由对应的 JMS服务厂商提供
    @Bean
    public ActiveMQConnectionFactory activeMQConnectionFactory() {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
        StringBuilder brokerURL = new StringBuilder( "tcp://" ).append( environment.getProperty( "jms.host" ) ).append( ":" ).append( environment.getProperty( "jms.port" ) );
        activeMQConnectionFactory.setBrokerURL( brokerURL.toString() );
        //按照apache官网的说明，为了避免收到恶意代码，引入了安全机制，只允许指定的包里的对象能够被传输
        activeMQConnectionFactory.setTrustAllPackages( true );// 解决办法 :使所有的类都能够被传输
//        activeMQConnectionFactory.setTrustedPackages(new ArrayList<String>( Arrays.asList( "POJO" ) ) );// 解决办法 :使指定的包里的类能够被传输
        return activeMQConnectionFactory;
    }

    @Bean
    public JmsTemplate jmsTemplate(ConnectionFactory connectionFactory) {
        JmsTemplate jmsTemplate = new JmsTemplate( connectionFactory );
        return jmsTemplate;
    }


    //声明一个queue
    @Bean
    public ActiveMQQueue activeMQQueue() {
        ActiveMQQueue activeMQQueue = new ActiveMQQueue( "hdd.queue" );
        return activeMQQueue;
    }

    @Bean
    public ActiveMQTopic activeMQTopic() {
        ActiveMQTopic activeMQTopic = new ActiveMQTopic( "hdd.topic" );
        return activeMQTopic;
    }


    // Spring用于管理真正的ConnectionFactory的ConnectionFactory
    @Bean
    @Primary
    public CachingConnectionFactory cachingConnectionFactory(ConnectionFactory connectionFactory) {
        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(connectionFactory);
        return cachingConnectionFactory;
    }

    //消息监听器
    @Bean
    public DefaultMessageListenerContainer messageListenerContainer(ConnectionFactory connectionFactory) {
        DefaultMessageListenerContainer messageListenerContainer = new DefaultMessageListenerContainer();
        messageListenerContainer.setConnectionFactory(connectionFactory);
        MessageListenerAdapter messageListenerAdapter = new MessageListenerAdapter( new  UserMDB()  );
        messageListenerAdapter.setDefaultListenerMethod( "receiveUser" );
        messageListenerAdapter.setDefaultResponseTopicName("hdd.topic");
        messageListenerContainer.setMessageListener( messageListenerAdapter);
        return messageListenerContainer;
    }


    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }
}
